package com.hzd.service;

import ablaze.framework.api.result.ExecuteResult;
import ablaze.framework.security.apitenant.ApiTenantDAO;
import ablaze.framework.service.BaseService;
import com.hzd.entity.ApiTenant;
import org.springframework.stereotype.Component;

/**
 * 接口租户
 *
 * @author christ
 * @date 6/24/2015
 */
@Component
public class ApiTenantService extends BaseService implements ApiTenantDAO {

    /**
     * 判断某个apiKey是否有效
     *
     * @param apiKey
     * @return
     */
    public boolean validate(String apiKey) {
        return findByApiKey(apiKey) != null;
    }

    /**
     * 根据apiKey查找
     *
     * @param apiKey
     * @return
     */
    public ApiTenant findByApiKey(String apiKey) {
        return find(ApiTenant.class).where().idEq(apiKey).findUnique();
    }

    /**
     * 注册
     *
     * @param entity
     * @return
     */
    public ExecuteResult register(ApiTenant entity) {
        store(entity);
        return success(entity);
    }

    @Override
    public ablaze.framework.security.apitenant.ApiTenant find(String apikey) {
        return find(ApiTenant.class).where().idEq(apikey).findUnique();
    }
}
